package com.hs.srm.admin.modules.material.service.impl;


import java.util.List;
import java.util.Map;

import com.hs.srm.base.common.utils.DictDataUtil;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.ObjectUtils;

import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.baomidou.mybatisplus.plugins.Page;
import com.baomidou.mybatisplus.service.impl.ServiceImpl;
import com.hs.srm.admin.modules.material.dao.MaterialFactoryDao;
import com.hs.srm.admin.modules.material.entity.MaterialFactoryEntity;
import com.hs.srm.admin.modules.material.service.MaterialFactoryService;
import com.hs.srm.admin.modules.material.vo.MaterialVO;
import com.hs.srm.base.common.annotation.DataFilter;
import com.hs.srm.base.common.utils.Constant;
import com.hs.srm.base.common.utils.QueryParam;
import com.hs.srm.common.utils.PageUtils;

@Service("materialFactoryService")
public class MaterialFactoryServiceImpl extends ServiceImpl<MaterialFactoryDao, MaterialFactoryEntity> implements MaterialFactoryService {

    @Autowired
    private MaterialFactoryDao materialFactoryDao;

    @Override
    @DataFilter
    public PageUtils queryPage(Map<String, Object> params) {
        //获取物料号
        String materialCode = (String) params.get("materialCode");
        //获取物料工厂
        String werks = (String) params.get("werks");
        String kc=((String) params.get("kc"));
        String sql="";
        if("0".equals(kc)){
            sql="qunatity-usecont>0";
        }else if("1".equals(kc)){
            sql="qunatity-usecont=0";
        }
        else {
            sql="1=1";
        }
        Page<MaterialFactoryEntity> page = this.selectPage(
                new QueryParam<MaterialFactoryEntity>(params).getPage(),
                new EntityWrapper<MaterialFactoryEntity>()
                        .like(StringUtils.isNotBlank(materialCode), "material_code", materialCode)
                        .like("material_desc ", String.valueOf(params.get("materialDesc")))
                        .like("storage_location ", String.valueOf(params.get("storageLocation")))
                        .like(StringUtils.isNotBlank(werks), "werks", werks)
                        .eq("isdel ", 0)
                        .and(sql));

        return new PageUtils(page);
    }

    @Override
    public Page<MaterialVO> selectMaterialVOPage(int pageNo, int pageSize, MaterialVO model) {
        // 当前页，总条数 构造 page 对象
        Page<MaterialVO> page = new Page<MaterialVO>(pageNo, pageSize);
        return page.setRecords(this.baseMapper.selectMaterialVOPage(page, model));
    }

    @Override
    public boolean insertMaterial(MaterialFactoryEntity entity) {
        boolean flg = this.insert(entity);
        return flg;
    }
    /**
     *
     * @Description 根据物料号物料工厂查询数据
     * @Param [code, werks]
     * @Return  com.hs.srm.admin.modules.material.entity.MaterialFactoryEntity
     * @Author  socik
     * @Date 2019/5/30 9:49
     */
    
    @Override
    public MaterialFactoryEntity selectMaterialFactoryByCode(String code, String werks) {
        EntityWrapper<MaterialFactoryEntity> queryWrapper = new EntityWrapper<>();
        queryWrapper.eq("material_code", code);
        queryWrapper.eq("werks", werks);
        MaterialFactoryEntity entity = this.selectOne(queryWrapper);
        return entity;
    }
    /**
     *
     * @Description 根据物料号查询所有物料工厂数据
     * @Param [code]
     * @Return  java.util.List<com.hs.srm.admin.modules.material.entity.MaterialFactoryEntity>
     * @Author  socik
     * @Date 2019/5/30 9:50
     */
    
    @Override
    public List<MaterialFactoryEntity> selectMaterialFactoryListByCode(String code) {
        EntityWrapper<MaterialFactoryEntity> queryWrapper = new EntityWrapper<>();
        queryWrapper.eq("material_code", code);
        List<MaterialFactoryEntity> entities = this.selectList(queryWrapper);
        return entities;
    }
    /**
     *
     * @Description 物料删除方法
     * @Param [entity]
     * @Return  boolean
     * @Author  socik
     * @Date 2019/5/30 10:35
     */
    
    @Override
    public boolean deleteMaterialFactory(MaterialFactoryEntity entity) {
        boolean flg = this.updateById(entity);
        return flg;
    }


}
